<?php
/**
 * 微笑云Api控制器
 */

namespace Home\Controller;

use Home\Model\ApiModel;
use Home\Model\HomeModel;
use Think\Controller;

class ApiController extends HomeController
{

    private $_M;

    public function __construct()
    {
        parent::__construct();
        sendAjaxHeader();
        $this->_M = new ApiModel('order');
    }

    public function index()
    {
        $this->show('<style type="text/css">*{ padding: 0; margin: 0; } div{ padding: 4px 48px;} body{ background: #fff; font-family: "微软雅黑"; color: #333;font-size:24px} h1{ font-size: 100px; font-weight: normal; margin-bottom: 12px; } p{ line-height: 1.8em; font-size: 36px } a,a:hover{color:blue;}</style><div style="padding: 24px 48px;"> <h1>:)</h1><p>欢迎使用 <b>ThinkPHP</b>！</p><br/>版本 V{$Think.version}</div><script type="text/javascript" src="http://ad.topthink.com/Public/static/client.js"></script><thinkad id="ad_55e75dfae343f5a1"></thinkad><script type="text/javascript" src="http://tajs.qq.com/stats?sId=9347272" charset="UTF-8"></script>', 'utf-8');
    }

    /**
     * 获取所有的路线,时间
     */
    public function getAllRoute()
    {
        $data = D('addr')->field("addr_from,addr_to,addr_depart_time")->select();
        echo json_encode($data);

    }


    /**
     * 获取已经存在的出发地、目的地
     */
    public function getBookTicketsInfo()
    {
        $data = D('addr')->select();
        $fromArr = array();
        $toArr = array();
        foreach ($data as $v) {
            $fromArr[] = $v['addr_from'];
            $toArr[] = $v['addr_to'];
        }
        $fromArr = array_unique($fromArr);
        $toArr = array_unique($toArr);

        echo json_encode(array("addrFrom" => $fromArr, "addrTo" => $toArr));
    }

    /**
     * 获取未付款的订单
     */
    public function ajaxGetHadNoPayTicket()
    {
        $dbData = $this->_M->getHadNoPay();
        echo json_encode($dbData);
    }

    /**
     * 获取预约中的订单
     */
    public function ajaxGetOrdingTicket()
    {

    }

    /**
     * 获取已支付的订单
     */
    public function ajaxGetHadPayTicket()
    {

    }

    /**
     * 获取已取消的订单
     */
    public function ajaxGetHadCanelTicket()
    {

    }


    /**
     * 用户分享后访问的API
     * 当用户扫描指定用户的二维码是,扫描者OpenId和推荐人的id将会被传到本接口
     * $usrId= I('get.usrId'); 提供二维码的人的id
     * $scanPerson= I('get.scanPerson');  扫描者的openid
     * 69 分享着的id
     * o8VaK0nuBjrOaCuiX0p5eIa3W07A 扫描者的openid
     * 模拟api:
     * http://www.gzcjdx.cn/WeChat/Bus/index.php/Home/Api/shareApi/usrId/69/scanPerson/o8VaK0nuBjrOaCuiX0p5eIa3W07A
     */
    public function shareApi()
    {
        $usrId = I('get.usrId');
        $scanPerson = I('get.scanPerson');

        //逻辑:如果扫描二维码的用户存在,更新他的分享者id,否者保存数据库

        $_m = new HomeModel('user');
        if ($ckRes = $_m->checkUserIsExist($scanPerson) <= 0) {//用户不存在
            //用户不存在,添加
            $dbData = array(
                'share_id' => $usrId,
                'usr_open_id' => $scanPerson,
                'usr_create_time' => time(),
                'share_time' => time()
            );
            $_m->add($dbData);
        } else {//用户存在
            //TODO 如果扫描者已经是我的用户,则不做处理
//            $upDbData = array(
//                'usr_id'=>$ckRes,
//                'share_id' => $scanPerson,
//                'share_time' => time()
//            );
//            $_m->save($upDbData);
        }
        exit;
    }
}